<?php include_template('header');?>
<table width="700" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td width="200" valign="top">
    <?php if(!is_login()){ ?>	
	<a href="<?php echo $config['site_url'];?>/signup.php">Join</a><br><a href="<?php echo $config['site_url'];?>/login.php">Login</a><br>
	<a href="<?php echo $config['site_url'];?>/signup.php">SubmitTopic</a><br>
	<?php }else{ ?>	
	<a href="<?php echo $config['site_url'];?>/editprofile.php">EditProfile</a><br>
	<a href="<?php echo $config['site_url'];?>/add_topic.php">SubmitTopic</a><br>
	<a href="<?php echo $config['site_url'];?>/logout.php">Logout</a><br>
	<?php } ?>
      <br>
	  <strong>Categories</strong><br>
      <br>
	  <a href="<?php echo $config['site_url'];?>">View All</a><br>
      <?php cat_list();?>
	  <br>
    <br></td>
    <td valign="top">
	<?php
	include_logic('topic');
	include_logic('user');
	include_logic('cat');
	$topic = topic_find_one(array('_id'=>new MongoId($_GET['id'])));
	if(!$topic){
		header('Location:'.$config['site_url'].'/index.php');
		exit;
	}
	$user = user_find_one(array('_id'=>new MongoId($topic['user_id'])));
	$cat = cat_find_one(array('_id'=>new MongoId($topic['cat_id'])));
	$comments = $topic['comment'];
	?>
	<table width="100%" cellpadding="0" cellspacing="0">
	<tr><td width="60" align="left" valign="top"><table border="0" cellspacing="0" cellpadding="0"><tr><td background="<?php echo $config['site_url'];?>/templates/<?php echo $config['template'];?>/images/digg_back.jpg"><table width="52" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td align="center" height="28" valign="bottom"><div class="digs" id="digs_<?php echo $topic['_id'];?>"><?php echo $topic['score'];?></div></td>
      </tr>
	  <tr>
	  <td height="24" align="center" valign="middle"><span class="digg">digs</span></td>
	  </tr>
      <tr>
        <td height="2"></td>
      </tr>
      <tr>
           <td height="25" align="center"><div class="digit" id="digbut"><a href="javascript:void(0);" onclick="digit('<?php echo $topic['_id'];?>');">dig it</a></div></td>
      </tr>
    </table></td>
    </tr></table></td>
	<td valign="top">
	  <table width="100%" height="70"  border="0" cellpadding="0" cellspacing="0">
        <tr>
          <td valign="top"><a href="<?php echo $topic['url'];?>"><?php echo $topic['title'];?></a></td>
        </tr>
		<tr>
		<td height="20"><span class="subline">submitted by  <a href="#"><?php echo $user['username'];?></a> <?php echo time_taken(time()-$topic['entry_time']);?> ago</span></td>
		</tr>
        <tr>
          <td valign="top"><?php echo $topic['description'];?></td>
        </tr>
        <tr>
          <td>&nbsp;</td>
        </tr>
        <tr>
          <td valign="bottom"><span class="bottomlinks">Categories: <a href="<?php echo $config['site_url'];?>/cat.php?cat_id=<?php echo $topic['cat_id'];?>"><?php echo $cat['name'];?></a></span></td>
        </tr>
      </table></td></tr>
	</table>
	<br>
	<table width="100%" border="0" cellpadding="0" cellspacing="0" style="border-bottom: 1px solid #dedede;">
      <tr>
        <td><div id="tabsB">
            <ul>
            </ul>
        </div></td>
      </tr>
    </table>
  	<br>
	<strong>Comments</strong><br>
	<?php 
	foreach($comments as $comment){
	$user = user_find_one(array('_id'=>new MongoId($comment['user_id'])));
	?>
	<table width="100%" border="0" cellpadding="0" cellspacing="0"><tr>
	<td>
	<table width="100%" border="0" cellpadding="0" cellspacing="0">
	<tr>
	<td height="25" valign="middle" bgcolor="#e4edf4"><table width="100%"  border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td><table border="0" cellpadding="0" cellspacing="0">
          <tr>
            <td width="20" align="center"><img src="<?php echo $config['site_url'];?>/templates/<?php echo $config['template'];?>/images/noavatar16x16.gif"></td>
            <td class="comline"> by <a href="#"><?php echo $user['username'];?></a> <?php echo time_taken(time()-$comment['entry_time']);?> ago</td>
          </tr>
        </table></td>
      </tr>
    </table></td>
	</tr>
	<tr id="commdesc"><td><?php echo $comment['comment'] ;?></td></tr>
	</table>
	</td></tr>
	</table>
	<br>
	<?php } ?>
    <br>
    <br>
    <a name="comments"></a>
	<strong>Add your comment</strong><br>
	<?php if(!is_login()){?>
	Please <a href="<?php echo $config['site_url'];?>/login.php">Login</a> Or <a href="<?php echo $config['site_url'];?>/signup.php">Signup</a> to leave a comment
    <?php }else{ ?><form name="form1" method="post" action="<?php echo $config['site_url'];?>/topic.php?id=<?php echo $topic['_id'] ;?>" style="padding:0px;margin:0px;">
      <table width="100%"  border="0" cellspacing="0" cellpadding="0">
        <tr>
          <td><textarea name="comment" id="comment" style="width:95%;height:100px;"></textarea></td>
        </tr>
        <tr>
          <td><input type="submit" name="Submit" value="Add Comment"></td>
        </tr>
      </table>
      <br>
        </form>
		<?php } ?>
    <br>
    <br></td>
  </tr>
</table>
<?php include_template('footer');?>